<?php
	if(!isset($_REQUEST['ajax'])){ $_REQUEST['ajax'] = "";}
	if($_REQUEST['ajax']!=true) include 'header.tpl.php';
	//include 'project_panel.tpl.php';
?>
	<div class="row">
		<div class="col-lg-12">
			<ul class="shortcut-buttons-set">
				<li>
					<a class="shortcut-button" href='#project_view/pid=<?php echo $this->item['pId'];?>'><span>
					<i class="aicon-task_detail"></i>
					<?php echo $this->language['detail'];?></span></a>
				</li>
				<?php if(check_permission('project_salary_edit')): ?>
				<li>
					<a class="shortcut-button" href='#project_view/pid=<?php echo $this->item['pId'];?>&mod=salaries'><span>
					<i class="aicon-chietkhau"></i>
					Hoa hồng</span></a>
				</li>
				<?php endif;?>
				<?php if(check_permission('project_view') && $this->item['pParent'] == 0){ ?>
				<li>
					<a class="shortcut-button" href='#project_view/pid=<?php echo $this->item['pId'];?>&mod=bussiness'><span>
					<i class="aicon-chart"></i>
					<?php echo $this->language['bussiness'];?></span></a>
				</li>
				<?php }?>
				<?php if($this->item['pParent'] == 0):?>
				<li>
					<a class="shortcut-button" href='#project_create/pid=<?php echo $this->item['pId'];?>'><span>
					<i class="aicon-project_add"></i>
					Tạo dự án con</span></a>
				</li>
				<?php endif;?>
			</ul>
		</div>
	</div>
	<input type="hidden" id="pidhid" name="pidhid" value="<?php echo $this->item['pId'];?>">
	<div class="row">
		<div class="col-lg-12">
			<div class="panel">
				<div class="panel-heading">
					<div class="panel-title"><i class="icon-money"></i> <?php echo $this->language['salary_system'];?></div>
				</div>
				<div class="panel-body">
					<form action="javascript:setProjectCredit2();" class="form-horizontal">
						<div class="form-group">
							<label class="col-lg-2 control-label"><?php echo $this->language['project_type'];?></label>
							<div class="col-lg-4">
								<select id="project_line" class="form-control">
									<option value="0">Chọn loại dự án</option>
									<?php foreach($this->project_lines as $row):?>
									<option value="<?php echo $row['line_id'];?>" <?php echo ($row['line_id'] == $this->item['pLine']) ? "selected":"";?>><?php echo $row['line_name'];?></option>
									<?php endforeach;?>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-lg-2 control-label"><?php echo $this->language['project_price'];?></label>
							<div class="col-lg-3">
								<div class="input-group">
									<input class="form-control" type="text" name="pCredit" id="pCredit" value="<?php echo number_format($this->item['pCredit'],0,'.',',');?>" />
									<span class="input-group-addon">VNĐ</span>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-lg-2 control-label">Tổng lương hoa hồng</label>
							<div class="col-lg-3">
								<div class="input-group">
									<input class="form-control" type="text" name="pTotalProfit" id="pTotalProfit" value="<?php echo number_format($this->item['pTotalProfit'],0,'.',',');?>" />
									<span class="input-group-addon">VNĐ</span>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-lg-2 control-label">Tổng lương đã chia</label>
							<div class="col-lg-3">
								<div class="input-group">
									<input class="form-control" type="text" disabled value="<?php echo number_format($this->item['pTotalProfitPushed'],0,'.',',');?>" />
									<span class="input-group-addon">VNĐ</span>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-lg-2 control-label">Còn lại</label>
							<div class="col-lg-3">
								<div class="input-group">
									<input class="form-control" type="text" disabled value="<?php echo number_format($this->item['pTotalProfit']-$this->item['pTotalProfitPushed'],0,'.',',');?>" />
									<span class="input-group-addon">VNĐ</span>
								</div>
							</div>
						</div>
						<div class="form-group">
							<label class="col-lg-2 control-label">Lương chia hoa hồng</label>
							<div class="col-lg-3">
								<div class="input-group">
									<input class="form-control" type="text" name="pProfitCredit" id="pProfitCredit" value="<?php echo number_format($this->item['pProfitCredit'],0,'.',',');?>" />
									<input type="hidden" id="project-profit" value="<?php echo $this->item['pProfitCredit'];?>" />
									<span class="input-group-addon">VNĐ</span>
								</div>
							</div>
						</div>
						<?php if(check_permission('project_salary_edit')){ ?>
						<div class="form-group">
							<div class="col-lg-offset-2 col-lg-4">
								<button class="col-offset-4 btn btn-primary" type="submit"><?php echo $this->language['update'];?></button>
							</div>
						</div>
						<?php } ?>
					</form>
				</div>
			</div>
		</div>
	</div>
	<div class="row">
		<div class="col-lg-12">
			<div class="panel">
				<div class="panel-heading">
					<div class="panel-title"><i class="icon-money"></i> Bảng chia hoa hồng</div>
				</div>
				<div class="panel-body">
					<form action="javascript:;" class="form-inline" id="percent-credit">
						<input type="hidden" name="mod" value="setPercentCredit2" />
						<input type="hidden" name="pid" value="<?php echo $this->item['pId'];?>" />
						<div class="form-row">
							<table class="table table-condensed">
								<thead>
									<tr>
										<th style="min-width:150px;"></th>
										<th width="230px"></th>
										<?php foreach($this->type_groups as $group):?>
										<th class="text-center" width="100px"><?php echo $group['group_name']." ".$group['group_percent']."%";?></th>
										<?php endforeach;?>
										<th width="25px"></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td></td>
										<td></td>
										<?php foreach($this->type_groups as $group):?>
										<td class="text-center"><?php echo number_format(($this->item['pProfitCredit']*$group['group_percent']/100),0,'.',',');?></td>
										<?php endforeach;?>
									</tr>
									<?php foreach($this->user_salaries as $row): ?>
									<tr class="r<?php echo $row['psal_id'];?>" rel="<?php echo $row['psal_id'];?>">
										<td class="text-middle"><?php echo $_SESSION['user_data'][$row['psal_user_id']]['fullname'];?></td>
										<td class="text-middle">
											<div class="form-group">
												<input type="hidden" name="psalgrs[]" value="<?php echo $row['psal_id'];?>" />
												<input type="hidden" name="amounts[]" class="input-amount" value="<?php echo round($row['psal_amount'],0);?>" />
												<select class="form-control select-group" name="groups[]">
													<option value="0">Chưa chọn</option>
													<?php foreach($this->type_groups as $group):?>
													<option value="<?php echo $group['group_id'];?>" rel="<?php echo $group['group_percent'];?>" <?php echo ($row['psal_group_id'] == $group['group_id']) ? "selected" : "";?>><?php echo $group['group_name'];?></option>
													<?php endforeach;?>
												</select>
											</div>
											<div class="form-group">
												<input type="text" id="input-percent" name="percents[]" class="form-control input-small input-percent" style="width:70px;" value="<?php echo $row['psal_percent'];?>" />
												<span>%</span>
											</div>
										</td>
										<?php foreach($this->type_groups as $group):?>
										<td class="text-center text-middle c<?php echo $group['group_id'];?> type_group" style="background:#<?php echo ($row['psal_group_id'] == $group['group_id']) ? $group['group_color'] : "FFFFFF"; ?>;">
											<?php echo ($row['psal_group_id'] == $group['group_id']) ? number_format(round($row['psal_amount'],0),0,'.',',') : "-"; ?>
										</td>
										<?php endforeach;?>
										<td><a href="javascript:;" onClick="del_user_salary(<?php echo $row['psal_id'];?>);"><img src="/resources/images/icons/cross.png" /></a></td>
									</tr>
									<?php endforeach;?>
								</tbody>
							</table>
						</div>
						<div class="form-row text-center">
							<button class="btn btn-primary" onClick="setPercentCredit2();"><i class="icon-ok"></i> Đồng ý</button>
							<button class="btn btn-danger" data-toggle="modal" href="#push_salary"><i class="icon-share"></i> Chia vào lương</button>
							<button class="btn btn-warning" data-toggle="modal" href="#more_salary"><i class="icon-user"></i> Thêm người</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	<?php if(count($this->histories) > 0): ?>
	<div class="row">
		<div class="col-lg-12">
			<div class="panel">
				<div class="panel-heading">
					<div class="panel-title"><i class="icon-money"></i> Lịch sử chia hoa hồng</div>
				</div>
				<div class="panel-body">
					<?php foreach($this->histories as $history):?>
					<h4 class="text-center">Chia hoa hồng ngày <?php echo date("d/m/Y",$history['his_create_date']);?></h4>
					<table class="table table-condensed table-bordered">
						<thead>
							<tr>
								<th></th>
								<th></th>
								<?php foreach($this->type_groups as $group):?>
								<th class="text-center"><?php echo $group['group_name']." ".$group['group_percent']."%";?></th>
								<?php endforeach;?>
							</tr>
						</thead>
						<tbody>
							<?php foreach($history['members'] as $row): ?>
							<tr>
								<td class="text-middle"><?php echo $_SESSION['user_data'][$row['his_user_id']]['fullname'];?></td>
								<td class="text-middle">
									<?php echo $row['his_percent'];?> %
								</td>
								<?php foreach($this->type_groups as $group):?>
								<td class="text-center text-middle type_group" style="background:#<?php echo ($row['his_group_id'] == $group['group_id']) ? $group['group_color'] : "FFFFFF"; ?>;">
									<?php echo ($row['his_group_id'] == $group['group_id']) ? number_format(round($row['his_amount'],0),0,'.',',') : "-"; ?>
								</td>
								<?php endforeach;?>
							</tr>
							<?php endforeach;?>
						</tbody>
					</table>
					<?php endforeach;?>
				</div>
			</div>
		</div>
	</div>
	<?php endif;?>
	<div class="modal fade" id="more_salary">
		<div class="modal-dialog">
			<div class="modal-content">
				<form action="javascript:more_salary();" id="more_user">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Thêm nhân viên vào bảng hoa hồng</h4>
					</div>
					<div class="modal-body">
						<fieldset>
							<div class="form-group">
								<label for="noteTitle">Chọn nhân viên</label>
								<select class="form-control" name="user[]" multiple size=7>
									<?php foreach($this->rooms as $row):?>
									<optgroup label="<?php echo $row['rName'];?>">
										<?php foreach($row['users'] as $user):?>
										<option value="<?php echo $user['uId'];?>"><?php echo $_SESSION['user_data'][$user['uId']]['fullname'];?></option>
										<?php endforeach;?>
									</optgroup>
									<?php endforeach;?>
								</select>
							</div>
						</fieldset>
					</div>
					<div class="modal-footer">
						<input type="hidden" name="mod" value="more_user" />
						<input type="hidden" name="project" value="<?php echo $this->item['pId'];?>" />
						<input type="submit" class="btn btn-primary" value="Đồng ý" />
					</div>
				</form>
			</div>
		</div>
	</div>
	<div class="modal fade" id="push_salary">
		<div class="modal-dialog">
			<div class="modal-content">
				<form action="javascript:push_salary();" id="more_user" class="form-inline">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Chia lương hoa hồng</h4>
					</div>
					<div class="modal-body">
						<fieldset>
							<div class="form-group">
								<label for="noteTitle">Vào tháng : </label>
								<select name="pushMonth" id="pushMonth" class="form-control control-inline">
									<?php for($i=1; $i <= 12;$i++){ ?>
									<option value="<?php echo $i;?>" <?php if($i == date("n",time())){ echo "selected";}?>>Tháng <?php echo $i;?></option>
									<?php } ?>
								</select>
								<select name="pushYear" id="pushYear" class="form-control control-inline">
									<?php for($i=2013; $i <= 2020;$i++){ ?>
									<option value="<?php echo $i;?>" <?php if($i == date("Y",time())){ echo "selected";}?>><?php echo $i;?></option>
									<?php } ?>
								</select>
							</div>
						</fieldset>
					</div>
					<div class="modal-footer">
						<input type="hidden" name="mod" value="more_user" />
						<input type="hidden" name="project" value="<?php echo $this->item['pId'];?>" />
						<input type="submit" class="btn btn-primary" value="Đồng ý" />
					</div>
				</form>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {
			$(".select-group").change(function(){
				$(this).parent().children(".input-percent").val(0);
				$(this).parent().parent().children(".type_group").html("-");
			});
			$(".input-percent").keyup(function(){
				var val = $(this).val();
				var per = $(this).parent().children(".select-group").find('option:selected').attr('rel');
				var row = $(this).parent().parent().attr('rel');
				var col = $(this).parent().children(".select-group").find('option:selected').val();
				var total = $("#project-profit").val();
				var amnt = (total * per / 100) * parseFloat(val) / 100;
				$(this).parent().children(".input-amount").val(Math.round(amnt*1000)/1000);
				$(".r"+row+" .c"+col).html(formatCurrency(Math.round(amnt*1000)/1000));
			});

			calc_salary();
		});

		function calc_salary(){
			$(".input-percent").each(function(){
				var val = $(this).val();
				var per = $(this).parent().children(".select-group").find('option:selected').attr('rel');
				var row = $(this).parent().parent().attr('rel');
				var col = $(this).parent().children(".select-group").find('option:selected').val();
				var total = $("#project-profit").val();
				var amnt = (total * per / 100) * parseFloat(val) / 100;
				$(this).parent().children(".input-amount").val(amnt);
				$(".r"+row+" .c"+col).html(formatCurrency(amnt));
			});
		}
	</script>
<?php if(isset($this->breadcrumb) && isset($this->title_header)):?>
	<script type="text/javascript">
		$(function() {
			$("#title-header").text('<?php echo $this->title_header;?>');
			$("#breadcrumb").html('<?php echo build_breadcrumb($this->breadcrumb);?>');
		});
	</script>
<?php endif;?>
<?php
	if($_REQUEST['ajax']!=true) include 'footer.tpl.php';
?>